<?php
class Module{
	public $app;
	public $dependencies = array();
	public $assets = array();
	
	public function Module(&$application){
		$this->app = $application;
		$this->check_dependencies();
		$this->load_dependencies();
		$this->load_assets();
	}
	
	private function check_dependencies(){
		foreach($this->dependencies as $d){
			if(!$this->app->module_loaded($d)){
				$this->app->err->fatal_error('Module <strong>'.$this->app->available_modules[$this->name].'</strong> requires <strong>'.$d.'</strong> to be loaded');
			}
		}
	}
	
	private function load_dependencies(){
		foreach($this->dependencies as $d){
			$this->$d = $this->app->$d;
		}
	}
	
	private function load_assets(){
		foreach($this->assets as $a){
			if(strstr($a, APP_FULL_DIR)){
				$file = $a;
			}else{
				$file = CORE_PATH.'modules/assets/'.$a.'.php';
			}
			if(!file_exists($file)){
				$this->app->err->dump_error('Asset <strong>'.$file.'</strong> does not exist.');
				continue;
			}
			require_once($file);
		}
	}
}
?>